package com.lnsoft.cac.client.test.nettyClient;

import com.lnsoft.cac.client.test.NettyClient;
import com.lnsoft.cac.server.handle.codec.I1Frame;
import com.lnsoft.cac.server.handle.codec.I1FrameDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ClientReadHandler extends SimpleChannelInboundHandler<ByteBuf> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        try {

            List<Object> out = new ArrayList<>();
            I1FrameDecoder.Decoder(byteBuf,out);
            if (out.isEmpty()) {
                return;
            }
            for (int i = 0; i < out.size(); i++) {
                I1Frame i1Frame = (I1Frame) out.get(i);
                if (i1Frame.getFrameType() == 0x02) {
                    //  心跳确认报文
                    if (i1Frame.getMsgType() == 0x01) {
//                        log.info("收到 >>> 心跳确认报文");
                        if (new String(i1Frame.getVoltageMonitorID()).equals("06M00000000011100")) {
//                            log.info("26M00090990010000:Heart");
//                            log.info(Long.toString());
                            List<Integer> l = NettyClient.listHeartTime;
                             l.add((int) (System.currentTimeMillis() - NettyClient.firstNodeHeartStartTime));
                            float av = 0;
                            int all = 0;
                            for (Integer iterm : l) {
                                all += iterm;
                            }
                            av = all / l.size();
                            log.info("HeartBeatTime:" + Float.toString(av));
                        }
                    } else {
                        log.info("心跳帧--报文类型错误");
                    }
                }

                if (i1Frame.getFrameType() == 0x04) {
                    //  心跳确认报文
                    if (i1Frame.getMsgType() == 0x04) {
                        log.info("收到 >>> 监测数据-电压数据-确认报文");
                    } else if (i1Frame.getMsgType() == 0x05) {
//                        log.info("收到 >>> 监测数据-日统计数据-确认报文");
                        if (new String(i1Frame.getVoltageMonitorID()).equals("06M00000000011100")) {
//                            log.info("26M00090990010000:Data");
//                            log.info(Long.toString());
                            List<Integer> l = NettyClient.listDDataTime;
                            l.add((int) (System.currentTimeMillis() - NettyClient.firstNodeDataTime));

                            float av = 0;
                            int all = 0;
                            for (Integer iterm : l) {
                                all += iterm;
                            }
                            av = all / l.size();
                            log.info("DataTime:" + Float.toString(av));
                        }
                    } else if (i1Frame.getMsgType() == 0x06) {
                        log.info("收到 >>> 监测数据-月统计数据-确认报文");
                    } else {
                        log.info("监测数据帧 -- 报文类型错误");
                    }
                }

            }

        } catch (Exception e) {
        }



    }
}

